/*
 Copyright (c) 2014 by Contributors

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package com.whirl.ai_ml_example;

import ml.dmlc.xgboost4j.java.Booster;
import ml.dmlc.xgboost4j.java.DMatrix;
import ml.dmlc.xgboost4j.java.XGBoost;
import ml.dmlc.xgboost4j.java.XGBoostError;

import java.util.HashMap;

import static com.whirl.util.Constants.XGBoost_PATH;

/**
 * example for start from a initial base prediction
 *
 * @author hzx
 */
public class BoostFromPrediction {
  public static void main(String[] args) throws XGBoostError {
    System.out.println("start running example to start from a initial prediction");

    // load file from text file, also binary buffer generated by xgboost4j
    DMatrix trainMat = new DMatrix(XGBoost_PATH+"demo/data/agaricus.txt.train");
    DMatrix testMat = new DMatrix(XGBoost_PATH+"demo/data/agaricus.txt.test");

    //specify parameters
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("eta", 1.0);
    params.put("max_depth", 2);
    params.put("silent", 1);
    params.put("objective", "binary:logistic");

    //specify watchList
    HashMap<String, DMatrix> watches = new HashMap<String, DMatrix>();
    watches.put("train", trainMat);
    watches.put("test", testMat);

    // 第一次训练
    //train xgboost for 1 round
    Booster booster = XGBoost.train(trainMat, params, 4, watches, null, null);

    float[][] trainPred = booster.predict(trainMat, true);
    float[][] testPred = booster.predict(testMat, true);

    // 第二次训练
    trainMat.setBaseMargin(trainPred);
    testMat.setBaseMargin(testPred);

    Booster booster2 = XGBoost.train(trainMat, params, 4, watches, null, null);

    float[][] trainPred2 = booster2.predict(trainMat, true);
    float[][] testPred2 = booster2.predict(testMat, true);


//    System.out.println("testPred.length="+testPred.length);
//    System.out.println("testPred[0].length="+testPred[0].length);
//    System.out.println("testPred2.length="+testPred2.length);
//    System.out.println("testPred2[0].length="+testPred2[0].length);

    System.out.println("第一次:");
    for (float[] pred : testPred) {
      for (float v : pred) {
        System.out.print(v + " ");
      }
    }
    System.out.println();
    System.out.println("第二次:");
    for (float[] pred : testPred2) {
      for (float v : pred) {
        System.out.print(v + " ");
      }
    }
    System.out.println();



    // 是在第一轮基础上又进行了一次，所以不一样
    System.out.println("检查预测结果：" + BasicWalkThrough.checkPredicts(testPred, testPred2));
  }
}
